package ch.sv7.tool.dbmanager.integration.sybase;

import java.lang.reflect.Field;

import junit.framework.TestCase;
import ch.sv7.tool.dbmanager.types.SybaseStaticTypesInfo;
import ch.sv7.tool.dbmanager.types.TypeInfo;
import ch.sv7.tool.dbmanager.types.TypesInfo;
import ch.sv7.tool.dbmanager.types.TypesInfoFactory;

/**
 * TODO: Document me!
 *
 * @author svt
 *
 */
public class SybaseStaticTypesInfoTestCase extends TestCase {

	public void testStaticSameAsConnection() throws Exception {
		TypesInfo connectionTypes = TypesInfoFactory.getInstance().getTypesInfo("sybase", ConnectionFactory.getTestConnection());
		TypesInfo staticTypes = new SybaseStaticTypesInfo();
		Field[] fields = TypeInfo.class.getDeclaredFields();
		String[] typesNames = connectionTypes.getTypeNames();
		int differences = 0; 
		for (int i=0; i<typesNames.length; i++) {
			String typeName = typesNames[i];		
			TypeInfo connectionType = connectionTypes.getTypeInfo(typeName);

			TypeInfo staticType = staticTypes.getTypeInfo(typeName);

			for (int j=0;j<fields.length; j++) {
				Field f = fields[j];
				f.setAccessible(true);
				Object connectionValue = f.get(connectionType);
				Object staticValue = f.get(staticType);
				if (connectionValue != null && staticValue != null) {
					if (!connectionValue.equals(staticValue)){
						differences++;
						System.out.println("Type '"+ typeName + "' field " + f.getName() + " are not equal");
					}
				} else if (connectionValue != null && staticValue == null){
					differences++;
					System.out.println("Type '"+ typeName + "' field " + f.getName() + " are not equal, static is null and should NOT");
				} else if (connectionValue == null && staticValue != null){
					differences++;
					System.out.println("Type '"+ typeName + "' field " + f.getName() + " are not equal, static is not null but should");
				}
			}
			
			
		}
		assertEquals("Types are different", 0, differences);
	}
}
